package com.zcgame.customview.fresh

import android.view.View

/**
 *@author:huangLJ
 *@date:2021/7/14 13:56
 */
class ProxyDownLoader(private var downLoader: DownLoader):DownLoader {

    private val listeners = HashSet<OnDownListener>(3)

    override fun createLoaderView(refreshLayout: PullRefreshLayout): View {
        return downLoader.createLoaderView(refreshLayout)
    }

    override fun getConfig(): DownLoader.LoaderConfig {
        return downLoader.getConfig()
    }

    override fun onDownBegin(refreshView: PullRefreshLayout) {
        downLoader.onDownBegin(refreshView)
        listeners.forEach{listener->
            listener.onDownBegin(refreshView)
        }
    }

    override fun onPositionChange(refreshView: PullRefreshLayout, status: Byte, dy: Int, currentDistance: Int) {
        downLoader.onPositionChange(refreshView, status, dy, currentDistance)
        listeners.forEach { listener->
            listener.onPositionChange(refreshView, status, dy, currentDistance)
        }
    }

    override fun onLoading(refreshView: PullRefreshLayout) {
        downLoader.onLoading(refreshView)
        listeners.forEach { listener->
            listener.onLoading(refreshView)
        }
    }

    override fun onReset(refreshView: PullRefreshLayout, pullRelease: Boolean) {
        downLoader.onReset(refreshView, pullRelease)
        listeners.forEach{ listener->
            listener.onReset(refreshView,pullRelease)
        }
    }

    override fun onPullLoadedFinish(pullRefreshLayout: PullRefreshLayout) {
        downLoader.onPullLoadedFinish(pullRefreshLayout)
        listeners.forEach { listener->
            listener.onPullLoadedFinish(pullRefreshLayout)
        }
    }

    fun addFootListener(listener:OnDownListener){
        if (!listeners.contains(listener)) {
            listeners.add(listener)
        }
    }
    fun removeFootListener(listener: OnDownListener){
        if (listeners.contains(listener)) {
            listeners.remove(listener)
        }
    }

    fun setFootView(downLoader: DownLoader) {
        this.downLoader = downLoader
    }
}